using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Module.CMS.Library;
using NCS.Module.CMS.Library.Distributions;
using NCS.Module.CMS.Library.Contents;
using NCS.Library.Utilities;
using NCS.Library;
using NCS.Module.CMS.Library.Events;
using System.IO;

namespace NCS.Module.CMS.Web.SubPortal
{
    public partial class Content_DetailSimlp : CMSWebControl
    {
        public int height = 0;
        protected string zoneurl = "";
        protected string zonename = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.zoneurl = this.BuildUrlByZoneSubPortal(this.curZone.Zone_ID, this.curZone.Portal_TabControlID);
                this.zonename = this.curZone.Zone_Name;

                if (curDistInfo == null)
                {
                    // get top 1 for not id content
                    DataTable dtinzone = DistributionController.GetDistributedContentsByZone(curZone.Zone_ID);
                    if (dtinzone == null) return;
                    if (dtinzone.Rows.Count > 0)
                    {
                        curDistInfo = DistributionController.GetDistribution(ConvertUtility.ToInt32(dtinzone.Rows[0]["Content_ID"].ToString()));
                    }
                }
                if (curDistInfo != null)
                {
                    DistributionController.UpdateDistributionView(curDistInfo.Distribution_ID, 1);
                }
                else
                {
                    return;
                }
                ContentInfo content = ContentController.GetContent(curDistInfo.Distribution_ContentID);

                if (content == null) return;

                if (content.Content_SubTitle != String.Empty)
                {
                    lblSubTitile.Text = content.Content_SubTitle + " <br><br>";
                }
                else
                {
                    lblSubTitile.Visible = false;
                }

                if (content.Content_Alias != string.Empty && content.Content_Alias != "")
                {

                    lbdate.Text = " - " + ConvertUtility.FormatTimeVn(curDistInfo.Distribution_CreateDate);
                }
                else
                {
                    lbdate.Text = ConvertUtility.FormatTimeVn(curDistInfo.Distribution_CreateDate);
                }



                lblSpecialHeadline.Text = content.Content_Headline;
                //litSpecialTeaser.Text = content.Content_Teaser;
                litSpecialContentBody.Text = content.Content_Body;
                lbdescavatar.Text = content.Content_AvatarDescription;
                hplPrintPreviews.NavigateUrl = "/portalid/" + AppEnv.PortalId() + "/tabid/144/catid/" + content.Content_ZoneID + "/distid/" + content.Content_ID + AppEnv.DEFAULT_EXTENTSION;
                hplSendMail.NavigateUrl = "/portalid/" + AppEnv.PortalId() + "/tabid/145/catid/" + content.Content_ZoneID + "/distid/" + content.Content_ID + AppEnv.DEFAULT_EXTENTSION;




                if (content.Content_Avatar != string.Empty)
                {
                    imgSpecialAvatar.ImageUrl = content.Content_Avatar;
                }
                else
                {
                    imgSpecialAvatar.Visible = false;
                   
                }



                
                DataTable dtPic = DistributionController.Distributions_GetPicInAlbumByContentId(curDistInfo.Distribution_ContentID);

                if (dtPic.Rows.Count > 0)
                {

                    string strAvatar = ConvertUtility.ToString(dtPic.Rows[0]["PicAvatar"]);
                    FileInfo fileInfo = new FileInfo(Server.MapPath(strAvatar));
                    System.Drawing.Image orImg = System.Drawing.Image.FromFile(fileInfo.FullName);
                    height = orImg.Height;


                }
                else
                {

                }

            }

        }
        protected void rptEvent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                HyperLink hplTitle = (HyperLink)e.Item.FindControl("hplTitle");

                hplTitle.Text = ConvertUtility.ToString(currow["Content_Headline"]);

                hplTitle.NavigateUrl = BuildUrlByDistId((int)currow["Content_ID"], curZone.Zone_ID);
            }
        }
        protected void rptNewContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                HyperLink hplTitle = (HyperLink)e.Item.FindControl("hplTitle");

                hplTitle.Text = ConvertUtility.ToString(currow["Content_Headline"]);

            }
        }
        protected void rptPic_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;

                Image imgSlide = (Image)e.Item.FindControl("imgSlide");
                if (ConvertUtility.ToString(currow["PicAvatar"]) != string.Empty)
                {
                    imgSlide.ImageUrl = currow["PicAvatar"].ToString();
                }
                else
                {
                    imgSlide.Visible = false;
                }

                Label lblDesc = (Label)e.Item.FindControl("lblDesc");
                if (ConvertUtility.ToString(currow["PicDetail"]) != string.Empty)
                {
                    lblDesc.Text = currow["PicDetail"].ToString();
                }
                else
                {
                    lblDesc.Visible = false;
                }
            }
        }
    }
}
